package com.tuzhi.filestore.controller;

import com.tuzhi.base.web.annotation.GlobalApiResultJson;
import com.tuzhi.filestore.config.FileStoreConfig;
import com.tuzhi.filestore.model.response.ErrorCode;
import com.tuzhi.filestore.model.response.Result;
import com.tuzhi.filestore.model.response.Results;
import com.tuzhi.filestore.websocket.server.WebSocketHelp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 功能说明：
 * 测试 发送消息使用
 *
 * @author abu
 * @version 1.1.0
 * @date 2023/2/21
 */
@Slf4j
@RestController
@RequestMapping("/ws")
@RequiredArgsConstructor
public class WebSocketController {

    @Autowired
    FileStoreConfig fileStoreConfig;

    /**
     * @param userName 用户名，连接到服务器的USERID
     * @param msg      消息体，发送到前端的消息
     * @return
     */

    @PostMapping(value = "/send/{userName}")
    public Result sendMsg(@PathVariable("userName") String userName, @RequestBody Map<String, Object> msg) {
        //log.warn("userName:{},msg:{}", userName, msg);
        try {
            Result result = WebSocketHelp.sendClientMsg(userName, msg);
            return result;
        } catch (Exception ex) {
            Result resultVO = Results.newFailResult(ErrorCode.FEIGIN_ERROR, ex.getMessage());

            return resultVO;
        }


    }

    @GetMapping(value = "/list")
    public Result listUser() {
        Result result = Results.newSuccessResult(WebSocketHelp.list());
        return result;

    }

    @GetMapping(value = "/test")
    @GlobalApiResultJson
    public String test() {
        return "ok===" + fileStoreConfig.getOrg();
    }

}
